//
//  CMDBLaunchScreenView.swift
//  CMDB
//
//  Created by 王磊 on 17/4/13.
//  Copyright © 2017年 CMDB. All rights reserved.
//

import UIKit

class CMDBLaunchScreenView: UIView {
    
    static func launchScreenView() -> CMDBLaunchScreenView {
        
        return CMDBLaunchScreenView(frame: UIScreen.mainScreen().bounds)
    }
    override init(frame: CGRect) {
        super.init(frame: frame)
        
        commitInit()
    }
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    private var imageView: UIImageView = UIImageView()
    
    private var versionLabel: UILabel = UILabel()
    
    private var skipItem: UIButton = UIButton(type: .Custom)
    
    private var timer: NSTimer?
}
extension CMDBLaunchScreenView {
    
    func commitInit() {
        
        backgroundColor = UIColor.whiteColor()
        
        alpha = 1
        
        imageView.contentMode = .ScaleAspectFit
        
        addSubview(imageView)
        
        versionLabel.textAlignment = .Center
        
        versionLabel.font = CMDB_12_FONT
        
        versionLabel.textColor = UIColor.blackColor()
        
        addSubview(versionLabel)
        
        versionLabel.text = CMDBApplicationManager.getInstance.localVersion
        
        addSubview(skipItem)
        
    }
}
extension CMDBLaunchScreenView {
    // 这里只提供本地图片
    internal func updateImage(image: UIImage?) {
        
        imageView.image = image
    }
    
}

extension CMDBLaunchScreenView {
    
    
    
}
extension CMDBLaunchScreenView {
    
    func startAnimation(superView: UIView) {
        
        //        UIApplication.sharedApplication().keyWindow?.addSubview(self)
        //
        //        UIApplication.sharedApplication().keyWindow?.bringSubviewToFront(self)
        
        imageView.alpha = 0.99
        
        versionLabel.alpha = 0.99
        
        UIView.animateWithDuration(1, animations: { [weak self] in
            
            self!.imageView.alpha = 1
            
            self!.versionLabel.alpha = 1
            
        }) { (isFinished) in
            
            UIView.animateWithDuration(2, animations: { [weak self] in
                
                self!.imageView.alpha = 0
                
                self!.versionLabel.alpha = 0
                
            }) { [weak self] (isFinished) in
                
                superView.removeFromSuperview()
                
                self!.removeFromSuperview()
                
                //
            }
        }
    }
    
    func dismiss() {
        
        UIView.animateWithDuration(1, animations: { [weak self] in
            
            self!.alpha = 0
            
        }) { [weak self ](flag) in
            
            if flag {
                
                self!.removeFromSuperview()
                
            }
        }
    }
}
extension CMDBLaunchScreenView {
    
    override func layoutSubviews() {
        super.layoutSubviews()
        
        imageView.frame = bounds
        
        versionLabel.snp_makeConstraints { (make) in
            
            make.left.equalTo(0)
            
            make.right.equalTo(0)
            
            make.bottom.equalTo(-30 * CMDB_Rate)
        }
    }
}
